home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / quartz / quartz10.lha / Pexample / StopWatch.h < prev   
C/C++ Source or Header  |  1990-05-14  |  3KB  |  129 lines

  1. //
  2. //  StopWatch - allows timing of code sections.  Current implementation
  3. //  supports timing of up to an unsigned long worth of microseconds, or
  4. //  about 71.5 minutes (2^32-1 = 4,294,nnn,nnn usecs = 4,294 secs = 71.5 mins).
  5. //
  6. //  Author: John Faust
  7. //  Date:   July 17, 1989.
  8. //
  9. //  Modification History:
  10. //
  11. //    Oct-10-1989  JEF
  12. //    Merged two similar files, yielding one StopWatch class with
  13. //    a single interface which utilizes either standard Unix functions
  14. //    (gettimeofday) or the sequent usec timer.
  15. //
  16.  
  17. #ifndef StopWatch_h
  18. #define StopWatch_h
  19.  
  20. //
  21. // The following #define was added to allow testing of both code paths
  22. // when on the Sequent.
  23. //
  24. #ifdef sequent
  25. #define NonPortableSW
  26. #endif sequent
  27.  
  28. #ifdef NonPortableSW
  29. #include <usclkc.h>
  30. #else
  31. #include <sys/time.h>
  32. #include <sys/resource.h>
  33. extern int gettimeofday (struct timeval*, struct timezone*);
  34. #endif NonPortableSW
  35.  
  36. #include <stream.h>
  37.  
  38. class STOPWATCH
  39. {
  40. #ifdef NonPortableSW
  41.     static shared_t usclk_t  StartTime1;
  42. #else
  43.     struct timeval           StartTime;
  44. #endif NonPortableSW
  45. public:
  46.     //
  47.     //  Constructor routine.
  48.     //
  49.     STOPWATCH ()
  50.     {
  51. #ifdef NonPortableSW
  52.         usclk_init ();
  53. #else
  54.         ;
  55. #endif NonPortableSW
  56.     };
  57.  
  58.     //
  59.     //  Destructor routine.
  60.     //
  61.     ~STOPWATCH ()
  62.     { ; };
  63.  
  64.     //
  65.     //  Start - start stopwatch.
  66.     //
  67.     void
  68.     Start ()
  69.     {
  70. #ifdef NonPortableSW
  71.         StartTime1 = getusclk ();
  72. #else
  73.         struct timezone zone;
  74.  
  75.         gettimeofday (&StartTime, &zone);
  76. #endif NonPortableSW
  77.     };
  78.  
  79.  
  80.     //
  81.     //  Mark - return time in usecs since Start.
  82.     //
  83.     unsigned long
  84.     Mark ()
  85.     {
  86. #ifdef NonPortableSW
  87.     usclk_t EndTime = getusclk ();
  88.     usclk_t Usecs;
  89.  
  90.     //
  91.     //  The sequent usec timer can wrap.  Check if wrap occurred and
  92.     //  recover.
  93.     //
  94.     if (EndTime > StartTime1)
  95.         Usecs = EndTime - StartTime1;
  96.         else
  97.     {
  98.         cout << form ("usec clock wrapped!\n");
  99.         Usecs = (0xffffffff - StartTime1) + EndTime;
  100.     }
  101. //    cout << form ("Usecs = %u\n", Usecs); cout.flush ();
  102.     return ( (unsigned long) Usecs );
  103.  
  104. #else
  105.  
  106.     struct timeval  EndTime;
  107.     struct timezone zone;
  108.     unsigned long usecs;
  109.  
  110.     gettimeofday (&EndTime, &zone);
  111.     usecs = ((EndTime.tv_sec - StartTime.tv_sec) * 1000000) +
  112.             (EndTime.tv_usec - StartTime.tv_usec);
  113.  
  114. //    cout << form ("Es=%u, Ss=%u, Eu=%u, Su=%u, R=%u\n",
  115. //              EndTime.tv_sec,
  116. //              StartTime.tv_sec,
  117. //              EndTime.tv_usec,
  118. //              StartTime.tv_usec,
  119. //              usecs);
  120. //    cout.flush ();
  121.  
  122.     return usecs;
  123. #endif NonPortableSW
  124.     };
  125.  
  126. };
  127.  
  128. #endif StopWatch_h
  129.